package com.haitao.wavebbs.controller.errorHandle;

import com.haitao.wavebbs.entity.tools.ResponseData;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author haitao
 * 全局Controller接口错误代理方法
 * @data 2023/2/23
 */
@ControllerAdvice
public class RestResonseEntityExceptionHandler  extends ResponseEntityExceptionHandler {
    @ExceptionHandler({Exception.class,ResponseStatusException.class})
    protected ResponseEntity<Object> handleConflict(Exception ex, WebRequest request){
        if (ex instanceof ResponseStatusException){
            ResponseStatusException statusException = (ResponseStatusException)ex;
            return new ResponseEntity(new ResponseData(statusException.getMessage(),null), statusException.getStatusCode());
        }
        //若未指定错误码，则status默认为400
        return new ResponseEntity(new ResponseData(ex.getMessage(),null), HttpStatus.BAD_REQUEST);
    }
}
